<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="text" id="i1">
<input type="text" id="i2">
<button onclick="f(1)">加</button>
<button onclick="f(2)">减</button>
<button onclick="f(3)">乘</button>
<button onclick="f(4)">除</button>
<h1>结果：<span></span></h1>
<script>
  let i1 = document.querySelector("#i1");
  let i2 = document.querySelector("#i2");
  let result = document.querySelector("span");
  function f(x) {
    if(isNaN(i1.value)||isNaN(i2.value)){
      result.innerText = "输入错误!请输入数字！";
      return;
    }
    switch (x){
      case 1:
        result.innerText = parseFloat(i1.value)+parseFloat(i2.value);
        break;
      case 2:
        result.innerText = i1.value-i2.value;
        break;
      case 3:
        result.innerText = i1.value*i2.value;
        break;
      case 4:
        result.innerText = i1.value/i2.value;
        break;
    }
  }
</script>
</body>
</html>